package com.chia.multienty.workflow.config;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.impl.AbstractEngineConfiguration;
import org.flowable.common.engine.impl.EngineConfigurator;

import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
public class DynamicEngineConfigurator implements EngineConfigurator {

    private static AtomicBoolean initialized = new AtomicBoolean();

    @Override
    public void beforeInit(AbstractEngineConfiguration abstractEngineConfiguration) {
        if(initialized.compareAndSet(false, true)) {
            DynamicDataSourceContextHolder.push("ds_workflow");
            log.info("已动态切换数据源...");
        }
    }

    @Override
    public void configure(AbstractEngineConfiguration abstractEngineConfiguration) {

    }

    @Override
    public int getPriority() {
        return 0;
    }
}
